package com.sky.controller.user;

import com.github.pagehelper.Page;
import com.sky.context.BaseContext;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author OZ.L.XO
 * #Description OrderController
 * #Date: 2025/7/11 17:55
 */
@RestController("userOrderController")
@RequestMapping("/user/order")
@Api(tags = "C端订单接口")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        log.info("用户下单：{}", ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }
    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    /**
    * @Description 订单提醒
    * @Param [id]
    * @Date 2025/7/31 23:05
    **/
    @GetMapping("/reminder/{id}")
    public Result<String> remind(@PathVariable Long id){
        log.info("订单提醒：{}", id);
        orderService.remind(id);
        return Result.success();
    }

    /**
    * @Description 再来一单
    * @Param [id]
    * @Date 2025/7/31 23:05
    **/
    @PostMapping("/repetition/{id}")
    public Result<String> repetition(@PathVariable Long id){
        log.info("再来一单：{}", id);
        orderService.repetition(id);
        return Result.success();
    }

    /**
    * @Description 查看历史订单
    * @Param [ordersPageQueryDTO]
    * @Date 2025/7/31 23:15
    **/
    @GetMapping("/historyOrders")
    public Result<PageResult> historyOrders(OrdersPageQueryDTO ordersPageQueryDTO){
        log.info("查看历史订单");
        ordersPageQueryDTO.setUserId(BaseContext.getCurrentId());
        PageResult pageResult = orderService.historyOrders(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
    * @Description 订单详情
    * @Param [id]
    * @Date 2025/7/31 23:15
    **/
    @GetMapping("/orderDetail/{id}")
    public Result<OrderVO> orderDetail(@PathVariable(value = "id") Long orderId){
        log.info("订单详情：{}", orderId);
        OrderVO orderVO = orderService.orderDetail(orderId);
        return Result.success(orderVO);
    }

    /**
    * @Description 用户取消订单
    * @Param [ordersCancelDTO]
    * @Date 2025/7/31 23:15
    **/
    @PutMapping("/cancel/{id}")
    public Result<String> cancel(@PathVariable("id") Long orderId){
        log.info("取消订单：{}", orderId);
        orderService.userCancle(orderId);
        return Result.success();
    }
}
